﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using Lvzhi.AdminCenter.Extension.MvcHelper;
using Lvzhi.Core;
using Lvzhi.Entities;
using Lvzhi.Entities.BaseEnum;
using Webdiyer.WebControls.Mvc;

namespace Lvzhi.AdminCenter.Controllers
{
    public class CompanyController : BaseController
    {
        private CompanyCore core = new CompanyCore();

        /// <summary>
        /// 承保公司
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int id = 1, int size = 10, string key = "")
        {

            Expression<Func<Core_Company, Boolean>> expr = t => true; 
             
            if (!string.IsNullOrWhiteSpace(key))
            {
                expr = expr.And(t => t.C_Name.Contains(key.Trim()));
            }
            var list = core.LoadEntities(expr).OrderByDescending(o => o.C_Createtime).ToPagedList(id, size);
            return View(list);
        }
        /// <summary>
        /// 编辑承保公司
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(Guid? id)
        {
            Core_Company obj = null;
            if (id.HasValue)
            {
                obj = core.LoadEntity(o=>o.C_Id == id);
            }
            else
            {
                obj = new Core_Company();
            }
            return View(obj);
        }

        [HttpPost]
        public ActionResult Edit(Core_Company obj)
        {
            bool flag = false;
            obj.C_Createtime = DateTime.Now;
            
            if (obj.C_Id  == Guid.Empty)
            {
                core.AddEntity(obj);
                flag = true;
            }
            else
            {
                flag =  core.UpdateEntity(obj);
            }
            if (flag)
            {

                return RedirectToAction("index");
            }
            return View(obj);
        }

        [HttpPost]
        public string Forzen(Guid id)
        {
            var info = core.LoadEntity(o => o.C_Id == id);

            if (info != null)
            {
                var state = info.C_State;
                if (state == (int)UserStatusEnum.冻结)
                {
                    state = (int)UserStatusEnum.正常;
                }
                else if (state == (int)UserStatusEnum.正常)
                {
                    state = (int)UserStatusEnum.冻结;
                }
                info.C_State = state;
                if (core.UpdateEntity(info)) //更改状态
                {
                    return "ok";
                }
            }
            return "更新数据失败";
        }


    }
}